//
//  SecondViewController.m
//  uplife
//
//  Created by ZhangCheng on 2018/12/20.
//  Copyright © 2018年 ZhangCheng. All rights reserved.
//

#import "SecondViewController.h"
#import "UIImageView+WebCache.h"
#import "WebViewController.h"
@interface SecondViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIPickerViewDelegate,UIPickerViewDataSource>
@property(nonatomic,strong)NSArray *dataArray;
@property(nonatomic,strong)IBOutlet UICollectionView *collectionView;
@property(nonatomic,assign)NSInteger currentIndex;
@property(nonatomic,strong)IBOutlet UIPickerView *pickerView;
@property(nonatomic,strong)IBOutlet UIButton *currentTypeBtn;
@property(nonatomic,strong)IBOutlet UIView *pickerViewSuperView;
@end

@implementation SecondViewController

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [ZCTool sharedInstance].infoTypeArray.count;
}

- (NSInteger)numberOfComponentsInPickerView:(nonnull UIPickerView *)pickerView {
    return 1;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [ZCTool sharedInstance].infoTypeArray[row][@"value"];
}

-(void)detailViewPickerViewDidClose{
    self.currentIndex = [self.pickerView selectedRowInComponent:0];
    [self.currentTypeBtn setTitle:[NSString stringWithFormat:@"%@ ▾",[ZCTool sharedInstance].infoTypeArray[self.currentIndex][@"value"]] forState:UIControlStateNormal];
    [self sendRequestForLoadData];
}
-(IBAction)showPickerView{
    [self.pickerView reloadAllComponents];
    self.pickerViewSuperView.alpha=0.0f;
    self.pickerViewSuperView.hidden = NO;
    [UIView animateWithDuration:0.25f animations:^{
        self.pickerViewSuperView.alpha=1.0f;
    }];
}

-(IBAction)closePickerView{
    [UIView animateWithDuration:0.25f animations:^{
        self.pickerViewSuperView.alpha=0.0f;
    } completion:^(BOOL finished) {
        self.pickerViewSuperView.hidden = YES;
        [self detailViewPickerViewDidClose];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"%@",[ZCTool sharedInstance].infoTypeArray);
    if ([ZCTool sharedInstance].infoTypeArray.count!=0) {
        self.currentIndex = 0;

        [self.currentTypeBtn setTitle:[NSString stringWithFormat:@"%@ ▾",[ZCTool sharedInstance].infoTypeArray[self.currentIndex][@"value"]] forState:UIControlStateNormal];
    }else{
        self.currentIndex=-1;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self sendRequestForLoadData];
}

-(void)sendRequestForLoadData{
    NSString *urlString;
    if (self.currentIndex==-1) {
        urlString = [NSString stringWithFormat:@"%@/info/list.do",JSXF_HOST];
    }else{
        urlString = [NSString stringWithFormat:@"%@/info/list.do?type=%@",JSXF_HOST,[ZCTool sharedInstance].infoTypeArray[self.currentIndex][@"code"]];

    }
    
    [SVProgressHUD show];
    
    [ZCWebServiceHelper requestWithMethod:POST andUrlStr:urlString andParameters:nil andFinished:^(id response, NSURLSessionDataTask *dataTask) {
        [SVProgressHUD dismiss];
        if (![response[@"data"] isKindOfClass:[NSString class]]) {
            NSDictionary *dic = response;
            self.dataArray = dic[@"data"][@"dataList"];
            [self.collectionView reloadData];
        }
        
    }];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSDictionary *dic = self.dataArray[indexPath.row];
    UIImageView *imageView = [cell viewWithTag:1];
    UILabel *titleLabel = [cell viewWithTag:2];
    UILabel *descLabel = [cell viewWithTag:3];
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",JSXF_HOST_FILE,dic[@"coverPic"]]]];
    titleLabel.text = [NSString stringWithFormat:@"%@",dic[@"title"]];
    descLabel.text = [NSString stringWithFormat:@"%@",dic[@"summary"]];
    
    
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = [UIScreen mainScreen].bounds.size.width - 20.0f;
    CGFloat height = width / 355.0f * 200.0f + 90.0f;
    return CGSizeMake(width, height);
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"toWeb"]) {
        WebViewController *vc = segue.destinationViewController;
        UICollectionViewCell *cell = sender;
        NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
        NSDictionary *dic = self.dataArray[indexPath.row];
        vc.requestURLString = [NSString stringWithFormat:@"%@/news.html?id=%@",JSXF_HOST_WEB_FILE,dic[@"id"]];
    }
}

@end
